package demo4.mapper;

import demo4.pojo.User;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

@Component
public class UserMapper implements IUserMapper {
    @Autowired
    private IDBUtil dbUtil;

    /**
     * 注册功能
     *
     * @param user
     * @return
     */
    @Override
    public int insert(User user) {
        System.out.println("正在注册中....");
        String sql = "INSERT INTO user(username, password, nickname, age) " +
                "VALUES (?,?,?,?);";
        try (Connection connection = dbUtil.getConnection()) {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getNickname());
            ps.setInt(4, user.getAge());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 注册时，查询用户名是否重复
     *
     * @param username
     * @return
     */
    @Override
    public User selectByUsername(String username) {
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "SELECT id,username,password,nickname,age " +
                    "FROM user " +
                    "WHERE username = ? ;";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                return user;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 登录功能
     *
     * @param username
     * @param password
     */
    @Override
    public void select(String username, String password) {
        System.out.println("信息校对中....");
        String sql = "SELECT username,password " +
                "FROM user " +
                "WHERE username = ? AND password = ?;";
        try (Connection connection = dbUtil.getConnection()) {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                System.out.println("尊敬的" + username + "用户，您已成功登录，欢迎使用本网站！");
            } else {
                System.out.println("密码错误！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @Override
    public int update(User user) {
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "UPDATE user " +
                    "SET password = ?,nickname = ?,age = ? " +
                    "WHERE username = ? ;";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getPassword());
            ps.setString(2, user.getNickname());
            ps.setInt(3, user.getAge());
            ps.setString(4, user.getUsername());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 查询所有用户的信息
     *
     * @return
     */
    @Override
    public List<User> selectAll() {
        List<User> list = new ArrayList<>();
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "SELECT id,username,password,nickname,age " +
                    "FROM user ";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                list.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 根据用户名删除指定用户的信息
     *
     * @param username
     */
    @Override
    public void deleteByUsername(String username) {
        try (Connection connection = dbUtil.getConnection()) {
            connection.setAutoCommit(false);
            String sql = "DELETE FROM user WHERE  username = ? ";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            int count = ps.executeUpdate();
            System.out.println("确认删除该用户吗？(1:确认，其他:取消)");
            int num = Integer.parseInt(new Scanner(System.in).nextLine());
            if (num == 1 && count > 0) {
                connection.commit();
                System.out.println("用户已删除");
            } else {
                connection.rollback();
                System.out.println("取消删除");
            }
            connection.setAutoCommit(true);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

